package com.jx.lcs.knowledge.service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jx.lcs.knowledge.dao.KnowledgeInfoDao;
import com.jx.lcs.knowledge.entity.KnowledgeInfo;
import com.jx.lcs.util.common.CharUtil;
import com.jx.lcs.util.common.GetProp;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.service.BaseService;

/**
 * @author 陈新雷 2015年7月2日下午4:13:24
 *
 */
@Service
@Transactional(readOnly = true)
public class KnowledgeInfoService extends BaseService<KnowledgeInfo, String> {
	@Autowired
	private KnowledgeInfoDao kid;

	@Override
	public HibernateDao<KnowledgeInfo, String> getEntityDao() {
		// TODO Auto-generated method stub
		return kid;
	}

	/**
	 * 得到全部数据
	 */
	public List<KnowledgeInfo> getAll() {

		return kid.findAll();

	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 */
	@Transactional(readOnly = false)
	public void delete(String id) {
		kid.delete(id);
	}

	public void createhtml(String title, String content,String nm) {
		try {
			PrintStream printStream = new PrintStream(new FileOutputStream(
					GetProp.getConfig("NEWS_HTML_UPLOAD_PATH")+nm), true, "UTF-8");
			StringBuffer html = new StringBuffer();
			html.append("<html>");
			html.append("<head>");
			html.append("<meta charset=\"utf-8\">");
			html.append("<meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\" />");
			html.append("<meta name=\"format-detection\" content=\"telephone=no\">");
			html.append("<meta name=\"format-detection\" content=\"address=no\">");
			
			//增加文章标题
			html.append("<title>").append(title).append("</title>");
			
			//添加意向客户引入样式
			html.append("<link href=\"http://www.zykmw.net:8080/lcs_news/news/jinxin/css/common.css\" rel=\"stylesheet\">");
			
			//增加微信分享脚本
			html.append("<script type=\"text/javascript\">");
			//分享标题
			html.append("var shareTitle = '无忧理财';");
			//分享描述
			html.append("var shareDesc = document.title;");
			//分享图标
			html.append("var imgUrl = document.location.protocol + '//' + document.location.host + '/lcs_icon/icon.png';");
			//分享链接
			html.append("var linkUrl = window.location.href;");
			
			html.append("</script>");
			
			//引入脚本
			html.append("<script src=\"http://res.wx.qq.com/open/js/jweixin-1.0.0.js\"> </script>");
			html.append("<script src=\"http://www.zykmw.net:8080/lcs_share/jquery-1.8.3.min.js\"> </script>");
			html.append("<script src=\"http://www.zykmw.net:8080/lcs_share/wx_share.js\"> </script>");
			
			//添加意向客户引入脚本
			html.append("<script src=\"http://www.zykmw.net:8080/lcs_news/news/jinxin/js/knowledge.js\"></script>");
			
			html.append("</head>");
			
			html.append("<body style=\"overflow-y:scroll;\">");
			
			//文章内容
			html.append(content);
			
			//意向客户信息输入
			html.append("<div id=\"userInfoDiv\" style=\"margin-top:-120px; display:none;\">");
			html.append("<div>");
			html.append("<div class=\"iscroll-page page-services-me\">");
			html.append("<div class=\"iscroll-page-body\">");
			html.append("<form class=\"form\">");
			html.append("<label>");
			html.append("<input id=\"username\" type=\"text\" maxlength=\"4\" placeholder=\"姓名\">");
			html.append("</label>");
			html.append("<label>");
			html.append("<input id=\"mobile\" type=\"text\" maxlength=\"11\" placeholder=\"手机号\">");
			html.append("</label>");
			html.append("<button class=\"btn\" type=\"button\" onclick=\"addIntentUser()\">确认</button>");
			html.append("</form>");
			html.append("</div>");
			html.append("</div>");
			html.append("</div>");
			html.append("</div>");
			
			html.append("</body>");
			
			html.append("</html>");
			
			String tem = html.toString();
			tem = CharUtil.charConvert(tem);
			printStream.print(tem);
			printStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
